import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '*',
    meta:{
      title:'404 Oops...'
    },
    component: ()=>import('../views/404.vue'),
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    meta:{
      title:'书城'
    },
    component: Home
  },
  {
    path: '/myinfo',
    name: 'User',
    meta:{
      title:'用户'
    },
    component: ()=> import('../views/User.vue'),
    beforeEnter: (to, from, next) => {
      // 用户是否登录
      if(to.params.logined){
        next()
      }else{
        next('/login')
      }
    }
  },
  {
    path: '/login',
    name: 'Login',
    meta:{
      title:'登录'
    },
    component: ()=> import('../views/Login.vue')
  },
  {
    path: '/more',
    name: 'More',
    component: ()=> import('../views/More.vue'),
    beforeEnter: (to, from, next) => {
      if(to.fullPath === '/more' || to.fullPath === '/more/top'){
        next('/404')
      }else{
        next()
      }
    },
    children:[
      {
        path:'hot',
        meta:{
          title:'热门',
          dataURL:'/booklist.php'
        },
        component: ()=> import('../views/MoreHot.vue')
      },
      {
        path:'top',
        component: ()=> import('../views/MoreTop.vue'),
        children:[
          {
            path:'week',
            meta:{
              title:'周榜',
              sign:'TOPWEEK',
              dataURL:'/booklist.php'
            },
            component: ()=> import('../views/Top.vue')
          },
          {
            path:'all',
            meta:{
              title:'总榜',
              sign:'TOPALL',
              dataURL:'/booklist.php'
            },
            component: ()=> import('../views/Top.vue')
          }
        ]
      },
      {
        path:'kind',
        meta:{
          title:'分类',
          dataURL:'/kindlist.php'
        },
        component: ()=> import('../views/MoreKind.vue')
      },
      {
        path:'list',
        meta:{
          title:'目录',
          dataURL:'/sectionlist.php'
        },
        component: ()=> import('../views/MoreList.vue')
      }
    ],
  },
  {
    path:'/kind/:id',
    name:'Kind',
    meta:{
      dataURL:'/kind-booklist.php'
    },
    component: ()=> import('../views/Kind.vue')
  },
  {
    path:'/bookdetail/:id',
    name:'BookeDetail',
    meta:{
      navBarTitle:'书籍详情',
      dataURL:'/bookdetail.php'
    },
    component: ()=> import('../views/BookDetail.vue')
  },
  {
    path:'/search',
    name:'Search',
    component: ()=> import('../views/Search.vue')
  },
  {
    path:'/article/:id',
    name:'Article',
    meta:{
      title:'在线阅读',
      dataURL:'/getcontent.php'
    },
    component: ()=> import('../views/ArticleContent.vue')
  },
  {
    path:'/recentread',
    name:'Recentread',
    meta:{
      title:'最近阅读',
      dataURL:'/nearread.php'
    },
    component: ()=> import('../views/RecentRead.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next) =>{
  document.title = to.meta.title ? to.meta.title+' - 喜阅' : ''
  next()
})
export default router
